﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Text;

namespace DaZhong.app_code
{
    public static class EnumTypes
    {
        public class ReadEnum
        {
            public string Name { get; set; }
            public int Value { get; set; }

        }

        public enum SourceIds
        {
            [Description("今日头条")]
            Toutiao = 1,
            [Description("今日头条-热点")]
            ToutiaoHotPoint = 2,
            [Description("今日头条-娱乐")]
            ToutiaoAmusements = 3,

            [Description("今日头条-社会")]
            ToutiaoSociety = 4,
            [Description("今日头条-财经")]
            ToutiaoFinance = 5,
            [Description("今日头条-军事")]
            news_military = 6,
            [Description("今日头条-汽车")]
            news_car = 7,
            [Description("今日头条-体育")]
            news_sports = 8,
            [Description("今日头条-国际")]
            news_world = 9,
            [Description("今日头条-时尚")]
            news_fashion=10,

            [Description("搜狗公众号")]
            WeiChat = 20,
            [Description("手动导入")]
            Import = 30,
            [Description("Api市场-头条")]
            AliApiTouTiao = 40,
            [Description("Api市场-新闻")]
            AliApiXinWen = 41

        }

        public enum ArticleStatus
        {
            /// <summary>
            /// 未审核
            /// </summary>
            [Description("未审核")]
            nonChecked = 0,
            /// <summary>
            /// 审核成功
            /// </summary>
            [Description("审核成功")]
            Success = 1,
            /// <summary>
            /// 停用
            /// </summary> 
            [Description("审核失败")]
            Failed = 2
        }


        ///<summary>
        /// 获得Enum类型description

        ///</summary>
        ///<param name="enumType">枚举的类型</param>
        ///<param name="val">枚举值</param>
        ///<returns>string</returns>
        public static string GetEnumDesc(Type enumType, object val)
        {
            string enumvalue = System.Enum.GetName(enumType, val);
            if (string.IsNullOrEmpty(enumvalue))
            {
                return "";
            }
            System.Reflection.FieldInfo finfo = enumType.GetField(enumvalue);
            object[] enumAttr = finfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
            if (enumAttr.Length > 0)
            {
                System.ComponentModel.DescriptionAttribute desc = enumAttr[0] as System.ComponentModel.DescriptionAttribute;
                if (desc != null)
                {
                    return desc.Description;
                }
            }
            return enumvalue;
        }



        ///<summary>
        /// 获取某个枚举的全部信息

        ///</summary>
        ///<typeparam name="T">枚举</typeparam>
        ///<returns>枚举的全部信息</returns>
        public static List<ReadEnum> GetEnumList<T>()
        {
            List<ReadEnum> list = new List<ReadEnum>();
            ReadEnum re = null;
            Type type = typeof(T);
            foreach (int enu in System.Enum.GetValues(typeof(T)))
            {
                re = new ReadEnum();
                re.Name = GetEnumDesc(type, enu);
                re.Value = enu;
                list.Add(re);
            }
            return list;
        }



        ///<summary>
        /// 根据值返回枚举对应的内容

        ///</summary>
        ///<typeparam name="T">枚举</typeparam>
        ///<param name="value">值(int)</param>
        ///<returns></returns>
        public static T GetModel<T>(int value)
        {
            T myEnum = (T)System.Enum.Parse(typeof(T), value.ToString(), true);
            return myEnum;
        }


    }
}
